package com.hzau.tourist_service_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.hzau.tourist_service_sys.dao.UserDao;
import com.hzau.tourist_service_sys.pojo.User;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Override
    public Result insert(User user) {
        //查询是否有用户名,电话号码，身份证是否重复
            String[] strlist = {"用户名", "电话号码", "身份证"};
            int[] list = {findByName(user.getUserName()), findByPhoneNumber(user.getPhoneNumber()), findByIdCard(user.getIdCard())};
            int temp=0;
        for (int j : list) {
            if (j == 1) {
                temp = 1;
                break;
            }if(j==2){
                return new Result(303,"注册信息不全",null);
            }
        }
        if(temp==1){
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < strlist.length; i++) {
                if (list[i] != 0) {
                    str.append(strlist[i]).append(" ");
                }
            }
            return new Result(302,str+"已用于注册",null);}
            userDao.insert(user);
        return Result.success();
    }

    @Override
    public Result findById(Long id) {
        User user = userDao.selectById(id);
        return Result.success(user);
    }

    @Override
    public User findByNameandPwd(String username, String password) {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName,username);
        lqw.eq(User::getPassword,password);

        return userDao.selectOne(lqw);
    }

    @Override
    public int findByName(String username) {
        if(username==null){
            return 2;
        }
        else if(userDao.findByName(username)!=null)
            return 1;
        else return 0;

    }

    @Override
    public int findByPhoneNumber(String PhoneNumber) {
        if(PhoneNumber==null){
            return 2;
        }else if(userDao.findByPhoneNumBer(PhoneNumber)!=null)
        return 1;

        else return 0;
    }

    @Override
    public int findByIdCard(String idCard) {
        if(idCard==null){
            return 2;
        }
       else if(userDao.findByIdCard(idCard)!=null)
            return 1;
        else return 0;
    }


}
